/** set immediately */
$(".datepicker").datepicker({
    changeMonth : true,
    changeYear : true,
    dateFormat : 'yy-mm-dd',
    showAnim : '',
});

$('[data-rel=tooltip]').tooltip();

/** ********************Modal Start*************************** */
// override dialog's title function to allow for HTML titles
$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
    _title : function(title) {
        var $title = this.options.title || '&nbsp;';
        if (("title_html" in this.options) && this.options.title_html == true)
            title.html($title);
        else
            title.text($title);
    }
}));

$.fn.extend({
    openModal : function(settings) {
        var divId = "_modal_dialg";
        // 防止静态页面缓存
        if (settings.random) {
            divId = "_modal_dialg" + Math.round(Math.random() * 100);
        }
        var defaultSettings = {
            id : divId,
            autoOpen : false,
            draggable : true,
            modal : true,
            resizable : false,
            title : "标题",
            title_html : true,
            bgiframe : true,
            // hide : "highlight",
            width : "800",
            height : "500",
            close : function() {
                $('#' + this.id).dialog('destroy');
                $("#" + this.id).remove();
            },
        };
        var modalSettings = $.extend({}, defaultSettings, settings);
        // 
        if (modalSettings.title) {
            modalSettings.title = "<div class='widget-header'><h4 class='smaller'>" + modalSettings.title
                    + "</h4></div>";
        }

        // default close button
        var closeButton = {
            html : "<i class='icon-remove bigger-110'></i>&nbsp; 关闭",
            "class" : "btn btn-xs",
            click : function() {
                $('#' + modalSettings.id).dialog("close");
            }
        };
        // 当设置中包含按钮时
        if (modalSettings.buttons) {
            // 添加关闭按钮
            modalSettings.buttons.push(closeButton);
        } else {
            // 只添加关闭按钮
            modalSettings.buttons = new Array(closeButton);
        }

        var url = settings.url;
        if (settings.url && settings.random) {
            if (settings.url.indexOf("?") != -1) {
                url = settings.url + "&" + Math.round(Math.random() * 100);
            } else {
                url = settings.url + "?" + Math.round(Math.random() * 100);
            }
        }

        var $modalDiv = $("<div id='" + modalSettings.id + "' class='_modal_dialg_class'></div>");
        var $iframe = $("<iframe id='_modal_frame' src=" + url
                + " frameborder='0' height='100%' width='100%' scrolling='auto' ></iframe>");
        $modalDiv.append($iframe);
        $modalDiv.appendTo($("body"));

        $('#' + modalSettings.id).dialog(modalSettings);
        // $(".ui-dialog-titlebar").hide();
        $('#' + modalSettings.id).dialog("open");
    },
    openXsModal : function(settings) {
        var defaultSettings = {
            width : "400",
            height : "300",
        };
        var modalSettings = $.extend({}, settings, defaultSettings);
        this.openModal(modalSettings);
    },
    openSmModal : function(settings) {
        var defaultSettings = {
            width : "800",
            height : "500",
        };
        var modalSettings = $.extend({}, settings, defaultSettings);
        this.openModal(modalSettings);
    },
    openMdModal : function(settings) {
        var defaultSettings = {
            width : "1024",
            height : "600",
        };
        var modalSettings = $.extend({}, settings, defaultSettings);
        this.openModal(modalSettings);
    }
});

$.extend({
    comfirm : function(setting) {
        var $div = $("<div class='_modal_confirm'><div class='ui-dialog-content'><p class='alert bigger-110  center grey'>" + setting.msg
                + "</p></div></div>");
        $div.appendTo($("body"));
        var iconClass = "icon-info-sign";
        if (setting.title_class) {
            iconClass = title_class;
        } else {
            if (setting.type == "info") {
                iconClass = "icon-info-sign red";
            } else if (setting.type == "warn") {
                iconClass = "icon-warning-sign red";
            }
        }

        var htmlTitle = "<div class='widget-header'><h4 class='smaller bolder'><i class='" + iconClass + "'></i>";
        if (setting.title) {
            htmlTitle += setting.title;
        } else {
            htmlTitle += "Confirm";
        }
        htmlTitle += "</h4></div>";
        $div.dialog({
            autoOpen : false,
            draggable : false,
            modal : true,
            resizable : false,
            title_html : true,
            bgiframe : true,
            width : "300",
            height : "160",
            close : function() {
                $(this).dialog('destroy');
                $(this).remove();
            },
            title : htmlTitle,
            buttons : setting.buttons ? setting.buttons : [ {
                html : "<i class='icon-ok bigger-110'></i>&nbsp; 确定",
                "class" : "btn btn-success btn-xs",
                click : function() {
                    if ($.isFunction(setting.functions.ok)) {
                        setting.functions.ok();
                    }
                    $(this).dialog("close");
                }
            }, {
                html : "<i class='icon-remove bigger-110'></i>&nbsp; 取消",
                "class" : "btn btn-xs",
                click : function() {
                    if ($.isFunction(setting.functions.cancel)) {
                        setting.functions.cancel();
                    }
                    $(this).dialog("close");
                }
            } ]
        });
        $div.dialog("open");
    }
});
/** ********************Modal End*************************** */

/** ********************行选中相关 Start*************************** */
// 行选中样式
var highlightClass = "ui-state-highlight";
// 行悬停样式
var hoverClass = "ui-state-hover";

/** 行悬停样式控制 */
$(".table tbody tr").hover(function() {
    // 行选中时不添加悬停样式
    if (!$(this).is("." + highlightClass)) {
        $(this).addClass(hoverClass);
    }
}, function() {
    $(this).removeClass(hoverClass);
});

/** 全选控制 */
$(".table thead :checkbox").click(function(e) {
    var $tr = $(this).closest("table").find("tbody tr");
    $tr.toggleClass(highlightClass);

    if ($(this).is(":checked")) {
        // 全部选中并加样式
        $tr.addClass(highlightClass);
        $tr.find(":checkbox").prop("checked", "checked");
    } else {
        // 全部不选中并加样式
        $tr.removeClass(highlightClass);
        $tr.find(":checkbox").removeAttr("checked");
    }
});

/** Checkbox选中控制 */
$(".table tbody :checkbox").click(function(e) {
    var $tr = $(this).closest("tr");
    $tr.toggleClass(highlightClass);
    if ($(this).is(":checked")) {
    } else {
        // 非选中时去掉全选
        $(this).closest("table").find("thead :checkbox").removeAttr("checked");
    }
    // 阻止行点击事件
    event.stopPropagation();
});

/** 行选中控制 */
$(".table tbody tr").click(function(e) {
    if ($(this).hasClass(highlightClass)) {
        // 行非选中时，去掉Checkbox选中
        $(this).find(":checkbox").removeAttr("checked");
        // 去掉全选选中
        $(this).closest("table").find("thead :checkbox").removeAttr("checked");
        // 去掉高亮样式
        $(this).removeClass(highlightClass);
    } else {
        // 去掉所有行高亮样式
        $(".table tbody tr").removeClass(highlightClass);
        // 去掉全选选中
        $(".table tbody :checkbox").removeAttr("checked");
        // Checkbox选中
        $(this).find(":checkbox").prop("checked", "checked");
        // 行高亮
        $(this).addClass(highlightClass);
    }
});
$(".table tbody tr:odd").addClass("ui-priority-secondary");
/** ********************行选中相关 End*************************** */

/** set onload */
$(function() {
    /*
     * $(".datepicker").next().on("click", function() { $(this).prev().focus();
     * });
     */
    $(".datepicker").click(function() {
        $(this).val("");
    });
});